package com.ohuang.test_compose.page

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.ClickableText
import androidx.compose.foundation.text.selection.DisableSelection
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import com.ohuang.test_compose.ContextUtil

@Composable
fun TextPage() {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .verticalScroll(rememberScrollState())
            , horizontalAlignment = Alignment.CenterHorizontally


    ) {

        Text(buildAnnotatedString {
            withStyle(style = SpanStyle(MaterialTheme.colors.primary)) {
                append("试试富文本")
            }
            append("He")
            // 改字体颜色
            withStyle(style = SpanStyle(MaterialTheme.colors.secondary)) {
                append("ll")
            }
            append("o World")
            append("富文本")
            // 加粗
            withStyle(style = SpanStyle(fontWeight = FontWeight.Bold)) {
                append("富文本加个粗")
            }
        })

        Spacer(modifier = Modifier.height(50.dp))

        SelectionContainer() {
            Column() {
                Text(
                    text = "可以选中的文本",
                    modifier = Modifier.fillMaxWidth(),
                    textAlign = TextAlign.Center
                )
                Spacer(modifier = Modifier.height(20.dp))
                DisableSelection {
                    Text(
                        text = "禁止选中的部分文本",
                        modifier = Modifier.fillMaxWidth(),
                        textAlign = TextAlign.Center
                    )
                }
            }
        }
        Spacer(modifier = Modifier.height(50.dp))
        ClickableText(
            text = buildAnnotatedString { append("可以点击的文本") },
            style = TextStyle(color = MaterialTheme.colors.onBackground)

        ) {
            ContextUtil.toast("第${it}个字符被点击了")
        }

        val annotatedText = buildAnnotatedString {
            withStyle(
                style = SpanStyle(
                    color = MaterialTheme.colors.onSurface
                )
            ) {
                append("试试链接吧，点击 ")
            }
            pushStringAnnotation(
                tag = "URL",
                annotation = "https://developer.android.com"
            )

            withStyle(
                style = SpanStyle(
                    color = MaterialTheme.colors.primary,
                    fontWeight = FontWeight.Bold
                )
            ) {
                append("这里")

            }
            pop()
            append(" 跳转")

        }
        Spacer(modifier = Modifier.height(50.dp))
        ClickableText(
            text = annotatedText,
            onClick = { offset ->
                annotatedText.getStringAnnotations(
                    tag = "URL", start = offset,
                    end = offset
                ).firstOrNull()?.let { annotation ->
                    ContextUtil.toast("点击链接${annotation.item}")
                }
            }
        )
        Spacer(modifier = Modifier.height(50.dp))
        var text5 by remember { mutableStateOf("") }
        BasicTextField(
            value = text5, onValueChange = { text5 = it },
            decorationBox = { innerTextField ->
                if (text5.isEmpty()) {
                    Box(contentAlignment = Alignment.CenterStart) {
                        Text(
                            text = "自定义样式输入框",
                            color = MaterialTheme.colors.primary
                        )
                    }
                }
                // 必须回调
                innerTextField()
            },
            modifier = Modifier
                .background(Color.LightGray, CircleShape)
                .padding(5.dp, 20.dp)
                .fillMaxWidth()

        )

    }


}